package com.imu.purse.task;

import com.imu.constant.Constant;
import com.imu.purse.mq.config.RabbitUtil;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component("ytfcoinTask")
public class YtfcoinTask {

    private static Log log = LogFactory.get(BitcoinTask.class);


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private YtfcoinRun ytfcoinRun;


    private static final  String transTime = RabbitUtil.getValue("ytf.trans.handle.interval");

    //定义一个执行器
    private static final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()+1);


    public void handleTradeInfo() {

        String expireTime = redisTemplate.opsForValue().get(Constant.CORE_MONEY_TRANS_EXPIRE);
        if (expireTime == null) {
            redisTemplate.opsForValue().set(Constant.CORE_MONEY_TRANS_EXPIRE, transTime);
        }
        //获取所有的核心钱包的地址
        List<String> coreList = redisTemplate.opsForList().range(Constant.CORE_MONEY_KEY, 0, -1);
        if(CollectionUtils.isEmpty(coreList)){
            return;
        }
        for (String coreStr : coreList) {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    ytfcoinRun.handleData(coreStr);
                }
            });
        }
    }
}
